//
//  HeadlineNewsViewController.m
//  WangYiNews
//
//  Created by lanou3g on 15/10/15.
//  Copyright (c) 2015年 zhengming. All rights reserved.
//

#import "HeadlineNewsViewController.h"
#import "NewsTwoTableViewCell.h"
#import "NewsOneTableViewCell.h"
#import "NewsHTMLViewController.h"

@interface HeadlineNewsViewController ()<UITableViewDataSource, UITableViewDelegate, SDCycleScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *headlineNewsArray;
@property (nonatomic, strong) NSMutableArray *headlineBannerArray;

@end

@implementation HeadlineNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    [self subView];
    [self setupData];
    
}

// 加载头条信息
- (void)setupData
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *url = [NSURL URLWithString:kHeadlineUrl];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
        [request setHTTPMethod:@"GET"];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSDictionary *tempDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *tempArray = tempDic[@"T1348647853363"];
        self.headlineBannerArray = [NSMutableArray array];
        self.headlineNewsArray = [NSMutableArray array];
        for (NSDictionary *dic in tempArray) {
            
            if ([[dic allKeys] containsObject:@"ads"]) {
                NSArray *array = dic[@"ads"];
                for (NSDictionary *adsDic in array) {
                    HeadlineNewsModel *model = [[HeadlineNewsModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [model setValuesForKeysWithDictionary:adsDic];
                    [self.headlineBannerArray addObject:model];
                }
            } else {
                HeadlineNewsModel *model = [[HeadlineNewsModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.headlineNewsArray addObject:model];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
    
}

// 布局视图
- (void)subView
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsOneTableViewCell" bundle:nil] forCellReuseIdentifier:@"cellOne"];
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"cellTwo"];
}

#pragma mark -实现UITableViewDataSource, UITableViewDelegate代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.headlineNewsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HeadlineNewsModel *model = self.headlineNewsArray[indexPath.row];
    if (model.imageArray.count > 1) {
       NewsTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellTwo" forIndexPath:indexPath];
        cell.twoTitleLabel.text = model.title;
        cell.twoReplyCountLabel.text = [NSString stringWithFormat:@"%@跟帖", model.votecount];
        [cell.twoImageViewOne sd_setImageWithURL:model.imageArray[0]];
        [cell.twoImageViewTwo sd_setImageWithURL:model.imageArray[1]];
        [cell.twoImageViewThree sd_setImageWithURL:model.imageArray[2]];
        return cell;
    } else {
        NewsOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellOne" forIndexPath:indexPath];
        [cell.oneImageView sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
        cell.oneTitleLabel.text = model.title;
        cell.oneDetailLabel.text = model.digest            ;
        cell.oneReplyCountLabel.text = [NSString stringWithFormat:@"%@跟帖", model.votecount];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    NSMutableArray *array = [NSMutableArray array];
    for (HeadlineNewsModel *model in self.headlineBannerArray) {
        [array addObject:model.imgsrc];
    }
    SDCycleScrollView *scrollview = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 120) imageURLStringsGroup:array];
    scrollview.autoScrollTimeInterval = 2;
    scrollview.delegate = self;
    [head addSubview:scrollview];
    
//    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, kScreenWidth, 30)];
    return head;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsHTMLViewController *newsVC = [[NewsHTMLViewController alloc] init];
    [self.navigationController pushViewController:newsVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HeadlineNewsModel *model = self.headlineNewsArray[indexPath.row];
    if (model.imageArray.count > 1) {
        return 130;
    } else {
        return 100;
    }
}

#pragma mark -实现轮播图代理方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
